'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url admin/pages/kh/yhsj/czjl/copyright_getList_for_ladder 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			formData,
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		// ================= 收益变更配置 =================
		if (!formData.querykey) {
			res.code = 1;
			res.msg = "请输入查询密钥"
			return res
		}
		if (!formData.tv_name) {
			res.code = 1;
			res.msg = "请输入剧名"
			return res
		}

		let tvInfo = await vk.baseDao.findByWhereJson({
			dbName: "a_tv",
			whereJson: {
				tv_name: formData.tv_name
			}
		});

		console.log("tvInfo", tvInfo)
		if (!tvInfo) {
			res.code = 1;
			res.msg = "系统中不存在该剧，请核对剧名";
			return res;
		}

		if (!tvInfo.querykey || tvInfo.querykey != formData.querykey) {
			res.code = 1;
			res.msg = "查询密钥错误";
			return res;
		}

		if (userInfo.role.includes("copyright") || userInfo.role.includes("admin")) {
			console.log("权限校验通过");
		} else {
			res.code = 1;
			res.msg = "用户身份校验不通过";
			return res;
		}


		// 查询收益变更固化配置
		let ladder_id = tvInfo.ladder_id;
		console.log("ladder_id", ladder_id)
		let ladder_static = tvInfo.ladder_static;
		console.log("ladder_static", ladder_static)
		if (!ladder_id) {
			console.log("需要初始化ladder_id")
			// 需要设置ladder_id
			ladder_static = await vk.baseDao.findByWhereJson({
				dbName: "a_ladder_config",
				whereJson: {
					tv_id: null
				}
			})

			if (!ladder_static) {
				res.code = 1;
				return res;
			}

			console.log("ladder_static", ladder_static)

			ladder_id = ladder_static._id;
			await vk.baseDao.updateById({
				dbName: "a_tv",
				id: tvInfo._id,
				dataJson: {
					ladder_static
				}
			});
		}

		// const pageData = await vk.callFunction({
		// 	url: 'admin/pages/kh/yhsj/czjl/copyright_getList',
		// 	data: {
		// 		formData
		// 	},
		// 	success: (data) => {
		// 		console.log(data)
		// 	},
		// 	uniIdToken: event.uniIdToken
		// });
		// console.log("pageData", pageData)

		// ================= 调用原有接口获取汇总数据 =================
		const countData = await vk.callFunction({
			name: "hedian",
			url: 'admin/pages/kh/yhsj/czjl/tongji3',
			data: {
				recharge_source: formData.tv_name
			},
			uniIdToken: event.uniIdToken
		});
		console.log("countData", countData)
		const count = vk.pubfn.toDecimal(countData.jrczje / 100, 2);

		// ================= 开始修改数据 =================
		// 排序保证正确
		const ladder_cut = ladder_static.ladder_cut
		ladder_cut.sort((a, b) => {
			return a.rangeStart < b.rangeStart;
		})
		console.log("ladder_cut", ladder_cut)
		let tagetLadderCut = null;
		for (let _index in ladder_cut) {
			const lc = ladder_cut[_index]
			const rangeStart = lc.rangeStart * 10000
			const rangeEnd = lc.rangeEnd * 10000
			console.log("rangeStart", rangeStart, "rangeEnd", rangeEnd, "count", count)
			if (rangeStart <= count && rangeEnd > count) {
				tagetLadderCut = lc;
			}
		}
		console.log("tagetLadderCut", tagetLadderCut)

		if (!tagetLadderCut) {
			res.code = 1;
			res.msg = "查询失败Z0014";
			return res;
		}

		// 总金额
		var _ladderCount = count;
		var ladderCount = 0;

		for (let _index in ladder_cut) {

			const lc = ladder_cut[_index]

			const v1 = lc.rangeEnd * 10000 - lc.rangeStart * 10000

			if ((_ladderCount - v1) >= 0) {
				ladderCount += v1 * lc.coefficient
			} else {
				ladderCount += _ladderCount * lc.coefficient
				break;
			}
			_ladderCount = _ladderCount - v1
			if (_ladderCount <= 0) {
				break;
			}
		}

		console.log("变更后的count: ", ladderCount)
		res._count = vk.pubfn.toDecimal(ladderCount, 2);

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}